package com.bonc.modules.dial.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bonc.datasource.annotation.DataSource;
import com.bonc.factory.PageFactory;
import com.bonc.modules.dial.dao.ServDialTestDataMonDao;
import com.bonc.modules.dial.entity.ServDialTestDataMon;
import com.bonc.modules.dial.service.ServDialTestDataMonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@DataSource("data")
@Service
public class ServDialTestDataMonServiceImpl extends ServiceImpl<ServDialTestDataMonDao, ServDialTestDataMon> implements ServDialTestDataMonService {

    @Autowired
    private ServDialTestDataMonDao servDialTestDataMonDao;

    @Override
    public Page<ServDialTestDataMon> queryPage(ServDialTestDataMon servDialTestDataMonParam){
        LambdaQueryWrapper<ServDialTestDataMon> queryWrapper = new LambdaQueryWrapper<>();
        // 日期
        if (ObjectUtil.isNotEmpty(servDialTestDataMonParam.getTestBeginTime())) {

            DateTimeFormatter destFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            DateTimeFormatter srcFormat = new DateTimeFormatterBuilder()
                    .appendPattern("yyyy-MM")
                    .parseDefaulting(ChronoField.DAY_OF_MONTH, 1)
                    .parseDefaulting(ChronoField.HOUR_OF_DAY, 0)
                    .toFormatter();
            LocalDateTime localDateTime = LocalDateTime.parse(servDialTestDataMonParam.getTestBeginTime(), srcFormat);
            String beginTimeStr = localDateTime.format(destFormat);
            String endTimeStr = localDateTime.plus(1, ChronoUnit.MONTHS).format(destFormat);
            queryWrapper.ge(ServDialTestDataMon::getTestBeginTime, beginTimeStr);
            queryWrapper.lt(ServDialTestDataMon::getTestBeginTime, endTimeStr);
        }
        // 指标类型
        if (ObjectUtil.isNotEmpty(servDialTestDataMonParam.getTestCode())) {
            //queryWrapper.like(ServDialTestDataMon::getTestCode, "_" + servDialTestDataMonParam.getTestCode());
            queryWrapper.apply("test_code like {0}", "_" + servDialTestDataMonParam.getTestCode());
        }
        // 网络类型
        if (ObjectUtil.isNotEmpty(servDialTestDataMonParam.getNetworkFlag())) {
            queryWrapper.eq(ServDialTestDataMon::getNetworkFlag, servDialTestDataMonParam.getNetworkFlag());
        }
        // 设备类型
        if (ObjectUtil.isNotEmpty(servDialTestDataMonParam.getDevType())) {
            queryWrapper.eq(ServDialTestDataMon::getDevType, servDialTestDataMonParam.getDevType());
        }
        List<String> proCodeList = new ArrayList<String>();
        // 产品，竞品
        if (ObjectUtil.isNotEmpty(servDialTestDataMonParam.getProCode())) {
            proCodeList.add(servDialTestDataMonParam.getProCode());
        }
        // 产品，竞品
        if (ObjectUtil.isNotEmpty(servDialTestDataMonParam.getCompetingProCode())) {
            proCodeList.add(servDialTestDataMonParam.getCompetingProCode());
        }
        if(ObjectUtil.isNotEmpty(proCodeList)){
            queryWrapper.in(ServDialTestDataMon::getProCode, proCodeList);
        }
        queryWrapper.orderByDesc(ServDialTestDataMon::getTestBeginTime);
        return baseMapper.selectPage(PageFactory.defaultPage(), queryWrapper);
    }

    @Override
    public List<String> queryDevType(String beginDate){

        QueryWrapper<ServDialTestDataMon> qw = new QueryWrapper<>();
        qw.select("distinct dev_type");
        LambdaQueryWrapper<ServDialTestDataMon> queryWrapper = qw.lambda();

        // 日期
        if (ObjectUtil.isNotEmpty(beginDate)) {
            DateTimeFormatter destFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            DateTimeFormatter srcFormat = new DateTimeFormatterBuilder()
                    .appendPattern("yyyy-MM")
                    .parseDefaulting(ChronoField.DAY_OF_MONTH, 1)
                    .parseDefaulting(ChronoField.HOUR_OF_DAY, 0)
                    .toFormatter();
            LocalDateTime localDateTime = LocalDateTime.parse(beginDate, srcFormat);
            String beginTimeStr = localDateTime.format(destFormat);
            String endTimeStr = localDateTime.plus(1, ChronoUnit.MONTHS).format(destFormat);
            queryWrapper.ge(ServDialTestDataMon::getTestBeginTime, beginTimeStr);
            queryWrapper.lt(ServDialTestDataMon::getTestBeginTime, endTimeStr);
        }
        queryWrapper.isNotNull(ServDialTestDataMon::getDevType);
        return baseMapper.selectObjs(queryWrapper).stream().map(o -> (String)o).collect(Collectors.toList());
    }

    @Override
    public Page<ServDialTestDataMon> selectPage(ServDialTestDataMon servDialTestDataMonParam){
        // 日期
        if (ObjectUtil.isNotEmpty(servDialTestDataMonParam.getTestBeginTime())) {

            DateTimeFormatter destFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            DateTimeFormatter srcFormat = new DateTimeFormatterBuilder()
                    .appendPattern("yyyy-MM")
                    .parseDefaulting(ChronoField.DAY_OF_MONTH, 1)
                    .parseDefaulting(ChronoField.HOUR_OF_DAY, 0)
                    .toFormatter();
            LocalDateTime localDateTime = LocalDateTime.parse(servDialTestDataMonParam.getTestBeginTime(), srcFormat);
            servDialTestDataMonParam.setBeginTimeStr(localDateTime.format(destFormat));
            servDialTestDataMonParam.setEndTimeStr(localDateTime.plus(1, ChronoUnit.MONTHS).format(destFormat));
        }
        // 指标类型
        if (ObjectUtil.isNotEmpty(servDialTestDataMonParam.getTestCode())) {
            servDialTestDataMonParam.setTestCode("_" + servDialTestDataMonParam.getTestCode());
        }
        List<String> proCodeList = new ArrayList<String>();
        // 产品，竞品
        if (ObjectUtil.isNotEmpty(servDialTestDataMonParam.getProCode())) {
            proCodeList.add(servDialTestDataMonParam.getProCode());
        }
        // 产品，竞品
        if (ObjectUtil.isNotEmpty(servDialTestDataMonParam.getCompetingProCode())) {
            proCodeList.add(servDialTestDataMonParam.getCompetingProCode());
        }
        if(ObjectUtil.isNotEmpty(proCodeList)){
            servDialTestDataMonParam.setProCodeList(proCodeList);
        }
        return servDialTestDataMonDao.page(PageFactory.defaultPage(), servDialTestDataMonParam);
    }
}
